package org.example;

/**
 * Copyright (c) 2023-2033 All Rights Reserved
 * InheritableThreadLocal 用法
 *
 * @date: 2023/5/9
 */
public class InheritableThreadLocalTest {
    private static InheritableThreadLocal<String> threadLocal = new InheritableThreadLocal<>();

    public static void main(String[] args) throws InterruptedException{
        Thread a = new Thread(() -> {
            InheritableThreadLocal<String> itl = new InheritableThreadLocal<>();
            itl.set("InheritableThreadLocal");

            Thread b = new Thread(() -> {
                String str = itl.get();
                //拿到父线程放进去的“InheritableThreadLocal”，因为tl是InheritableThreadLocal
                System.out.println(str);
            });
            b.start();

            //确保子线程b执行完毕
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }

            itl.remove();
        });

        a.start();
    }


}
